﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HelperPrototype.TabPageControls
{
    public partial class LapPhieuTamUng : UserControl
    {
        public LapPhieuTamUng()
        {
            InitializeComponent();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            OtherForms.ThemPhieuTamUng f = new OtherForms.ThemPhieuTamUng();
            f.ShowDialog();
            loadData();
        }

        private void LapPhieuTamUng_Load(object sender, EventArgs e)
        {
            loadData();
        }

        private void loadData()
        {
            _DB.DataClasses1DataContext dc = new _DB.DataClasses1DataContext();
            var listPhieu = from qt in dc.QuyTrinhMuaHangs
                            where qt.deleted == false &&
                                  qt.PhieuTamUng != null
                            select qt.PhieuTamUng;

            if (!Program.NguoiDungHienTai.coQuyen(38))
                listPhieu = listPhieu.Where(x => x.MaNguoiTao == Program.MaNguoiDungHienTai);

            dgvPhieuTamUng.Rows.Clear();
            int count = 1;
            foreach (_DB.PhieuTamUng p in listPhieu)
            {
                DataGridViewRow r = new DataGridViewRow();
                r.CreateCells(dgvPhieuTamUng);
                r.Cells[0].Value = count++;
                r.Cells[1].Value = p.MaPhieu;
                r.Cells[2].Value = p.NguoiTao.Ten;
                r.Cells[3].Value = p.NgayTao;
                r.Cells[4].Value = p.ThoiGian;
                r.Cells[5].Value = p.SoTien;
                r.Cells[6].Value = p.QuyTrinhMuaHangs.Single().TrangThaiQuyTrinhMuaHang.MoTa;
                r.Tag = p.QuyTrinhMuaHangs.Single();

                if (p.QuyTrinhMuaHangs.Single().TrangThai == _DB.TrangThaiQuyTrinhMuaHang.KHONG_DUYET_PHIEU_TAM_UNG)
                {
                    r.Cells[6].Value += " (" + p.LyDoKhongDuyet + ")";
                }

                dgvPhieuTamUng.Rows.Add(r);
            }
        }

        private void dgvPhieuTamUng_DoubleClick(object sender, EventArgs e)
        {
            if (dgvPhieuTamUng.SelectedRows.Count > 0)
            {
                string maPhieu = (string)dgvPhieuTamUng.SelectedRows[0].Cells[1].Value;
                OtherForms.XemPhieuTamUng f = new OtherForms.XemPhieuTamUng(maPhieu);
                f.ShowDialog();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            // check select
            if (dgvPhieuTamUng.SelectedRows.Count <= 0)
                return;

            // check trang thai
            _DB.QuyTrinhMuaHang qt = (_DB.QuyTrinhMuaHang)dgvPhieuTamUng.SelectedRows[0].Tag;
            if (qt.TrangThai != _DB.TrangThaiQuyTrinhMuaHang.CHINH_SUA_PHIEU_TAM_UNG &&
                qt.TrangThai != _DB.TrangThaiQuyTrinhMuaHang.KHONG_DUYET_PHIEU_TAM_UNG)
            {
                return;
            }

            if (MessageBox.Show("Chắn chắn?", "", MessageBoxButtons.YesNo) == DialogResult.No)
                return;

            // go
            string maPhieu = (string)dgvPhieuTamUng.SelectedRows[0].Cells[1].Value;
            _DB.DataClasses1DataContext dc = new _DB.DataClasses1DataContext();
            qt = dc.QuyTrinhMuaHangs.Single(x => x.MaPhieuTamUng == maPhieu);
            qt.TrangThai = _DB.TrangThaiQuyTrinhMuaHang.CHO_DUYET_PHIEU_TAM_UNG;
            dc.SubmitChanges();
            loadData();

        }

        private void button1_Click(object sender, EventArgs e)
        {
            // check select
            if (dgvPhieuTamUng.SelectedRows.Count <= 0)
                return;

            // check trang thai
            _DB.QuyTrinhMuaHang qt = (_DB.QuyTrinhMuaHang)dgvPhieuTamUng.SelectedRows[0].Tag;
            if (qt.TrangThai != _DB.TrangThaiQuyTrinhMuaHang.CHINH_SUA_PHIEU_TAM_UNG &&
                qt.TrangThai != _DB.TrangThaiQuyTrinhMuaHang.KHONG_DUYET_PHIEU_TAM_UNG)
            {
                return;
            }

            // go
            string maPhieu = (string)dgvPhieuTamUng.SelectedRows[0].Cells[1].Value;
            OtherForms.ThemPhieuTamUng f = new OtherForms.ThemPhieuTamUng(maPhieu);
            f.ShowDialog();
            loadData();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            // check select
            if (dgvPhieuTamUng.SelectedRows.Count <= 0)
                return;

            // check trang thai
            _DB.QuyTrinhMuaHang qt = (_DB.QuyTrinhMuaHang)dgvPhieuTamUng.SelectedRows[0].Tag;
            if (qt.TrangThai != _DB.TrangThaiQuyTrinhMuaHang.CHINH_SUA_PHIEU_TAM_UNG &&
                qt.TrangThai != _DB.TrangThaiQuyTrinhMuaHang.KHONG_DUYET_PHIEU_TAM_UNG)
            {
                return;
            }

            if (MessageBox.Show("Chắn chắn?", "", MessageBoxButtons.YesNo) == DialogResult.No)
                return;

            // go
            string maPhieu = (string)dgvPhieuTamUng.SelectedRows[0].Cells[1].Value;
            _DB.DataClasses1DataContext dc = new _DB.DataClasses1DataContext();
            qt = dc.QuyTrinhMuaHangs.Single(x => x.MaPhieuTamUng == maPhieu);
            _DB.PhieuTamUng p = dc.PhieuTamUngs.Single(x => x.MaPhieu == maPhieu);
            p.deleted = true;
            qt.MaPhieuTamUng = null;
            qt.TrangThai = _DB.TrangThaiQuyTrinhMuaHang.DANG_MUA_HANG;
            dc.SubmitChanges();
            loadData();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (dgvPhieuTamUng.SelectedRows.Count == 0)
                return;
            string maPhieu = (string)dgvPhieuTamUng.SelectedRows[0].Cells[1].Value;
            //MessageBox.Show(maPhieu);
            //Reports.DS_PhieuDeNghiTamUng ds = new Reports.DS_PhieuDeNghiTamUng();
            //ds.fillData(maPhieu);
            //ds.viewReport();
        }
    }
}
